﻿using StockTradTest.Data;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace StockTradTest
{
    public class Funciton
    {
        /// <summary>
        /// 判断是不是周末/节假日
        /// </summary>
        /// <param name="date">日期</param>
        /// <returns>周末和节假日返回true，工作日返回false</returns>
        public static async Task<bool> IsHolidayByDate(DateTime date)
        {
            var isHoliday = false;
            var webClient = new System.Net.WebClient();
            var PostVars = new System.Collections.Specialized.NameValueCollection
            {
                { "d", date.ToString("yyyyMMdd") }//参数
            };
            try
            {
                var day = date.DayOfWeek;

                //判断是否为周末
                if (day == DayOfWeek.Sunday || day == DayOfWeek.Saturday)
                    return true;

                //0为工作日，1为周末，2为法定节假日
                var byteResult = await webClient.UploadValuesTaskAsync("http://tool.bitefu.net/jiari/", "POST", PostVars);//请求地址,传参方式,参数集合
                var result = Encoding.UTF8.GetString(byteResult);//获取返回值
                if (result == "1" || result == "2")
                    isHoliday = true;
            }
            catch
            {
                isHoliday = false;
            }
            return isHoliday;
        }
        /// <summary>
        /// 更新所有的股票信息
        /// </summary>
        /// <param name="updateInfo">是否更新列表</param>
        /// <returns></returns>
        public static async Task UpdateAllAsync(bool updateInfo = false, bool reWriteExist = false)
        {
            if(updateInfo)
            {
                await UpdateInfo();
            }
            //如果不重写已存在的数据
            List<StockInfo> infos = Base.StockInfoDatabase.GetAllStockInfoAsync().Result;
            if (!reWriteExist)
            {
                foreach (StockInfo info in infos)
                {
                    Base.CurrentCode = info.Code;
                    var result = Base.StockDatabase.GetStockAsync().Result;
                    if (result == null || result.Count == 0)
                        await UpdateStockDay(info.Code);
                    else
                        Console.WriteLine(info.Code + "Exist");
                }
            }
            else
            {
                foreach (StockInfo info in infos)
                {
                    await UpdateStockDay(info.Code);
                }
            }

        }
        /// <summary>
        /// 更新股票列表
        /// </summary>
        /// <returns></returns>
        public static async System.Threading.Tasks.Task UpdateInfo()
        {
            await foreach (string str in GetStockInfo.UpdateInfoAsync())
            {
                Console.WriteLine(str);
            }
        }
        /// <summary>
        /// 更新单只股票信息
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public static async System.Threading.Tasks.Task UpdateStockDay(string code)
        {
            await foreach (string str in GetStockHistory.UpdateDayAsync(code))
            {
                Console.WriteLine(str);
            }
            //Console.WriteLine(code + " data downloaded");
        }
        /// <summary>
        /// 更新所有指数数据
        /// </summary>
        /// <returns></returns>
        public static async Task UpdateAllIndexAsync()
        {
            foreach (var key in Index.IndexDic.Keys)
            {
                Console.WriteLine(key);
                await UpdateIndexDay(key);
            }
        }
        /// <summary>
        /// 更新单个指数信息
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public static async System.Threading.Tasks.Task UpdateIndexDay(string code)
        {
            await foreach (string str in GetIndexHistory.UpdateDayAsync(code))
            {
                Console.WriteLine(str);
            }
            //Console.WriteLine(code + " data downloaded");
        }
        /// <summary>
        /// 查询股票信息
        /// </summary>
        /// <param name="code">股票代码</param>
        /// <param name="day">日期，默认0为最早日期</param>
        /// <returns></returns>
        public static string QueryStockInfo(string code, int day = 0)
        {
            Base.CurrentCode = code;
            var info = Base.StockInfoDatabase.GetStockInfoAsync(code).Result;
            if (info == null) return "股票列表中未查到该股票信息";
            if (day == 0 || day == 1)
            {
                var result = Base.StockDatabase.GetStockAsync().Result;
                if (result == null || result.Count == 0) return "股票数据库中未查到该股信息";
                if (day == 0)
                {
                    return info.Name + "\n" + result[0].Info();
                }
                return info.Name + "\n" + result[result.Count - 1].Info();
            }
            Stock stock = Base.StockDatabase.GetStockAsync(day).Result;
            if (stock == null) return "股票数据库中未查到该股该日期信息";
            return info.Name + "\n" + stock.Info();
        }
        /// <summary>
        /// 查询指数信息
        /// </summary>
        /// <param name="code">股票代码</param>
        /// <param name="day">日期，默认0为最早日期</param>
        /// <returns></returns>
        public static string QueryIndexInfo(string code, int day = 0)
        {
            Base.CurrentIndexCode = code;
            Index.IndexDic.TryGetValue(code, out string value);
            if (value == null) return "指数列表中未查到该指数信息";
            if (day == 0 || day == 1)
            {
                var result = Base.IndexDatabase.GetIndexAsync().Result;
                if (result == null || result.Count == 0) return "指数数据库中未查到该指数信息";
                if (day == 0)
                {
                    return value + "\n" + result[result.Count - 1].Info();
                }
                return value + "\n" + result[result.Count - 1].Info();
            }
            Stock stock = Base.StockDatabase.GetStockAsync(day).Result;
            if (stock == null) return "指数数据库中未查到该指数该日期信息";
            return value +  "\n" + stock.Info();
        }

        public static string QueryMyAccountInfo()
        {
            var account = Base.AccountDatabase.GetAccountAsync().Result;
            if (account == null) return "无账户信息";
            string result = account.Info();
            var myStocks = Base.MyStockDatabase.GetAllStockAsync().Result;
            foreach(MyStock myStock in myStocks)
            {
                result += "\n" + myStock.Info();
            }
            return result;
        }
        public static string QueryRealTimeStockInfo(string code)
        {
            RealTimeInfo info = GetRealTimeInfo.GetStockRealTimeInfo(code);
            if (info == null) return "未查到相关信息";
            return info.Info();
        }
        public static string QueryRealTimeIndexInfo(string code)
        {
            RealTimeInfo info = GetRealTimeInfo.GetIndexRealTimeInfo(code);
            if (info == null) return "未查到相关信息";
            return info.Info();
        }
        public static bool ResetMyAccount()
        {
            TestTradBase.ResetAccountAndStock();
            return true;
        }
    }
}
